#!/bin/bash
# 快速验证脚本 - 验证 mo 是否为静态链接

echo "========================================="
echo "  mo 可执行文件静态链接验证"
echo "========================================="
echo ""

MO_PATH="/home/apachee/trunk/Crystal/CrystalFlowAnalyzer/modb/mo"

if [ ! -f "$MO_PATH" ]; then
    echo "❌ 错误：找不到 mo 文件"
    echo "   路径：$MO_PATH"
    exit 1
fi

echo "✓ 文件存在"
echo ""

echo "【1】文件信息："
ls -lh "$MO_PATH"
echo ""

echo "【2】文件类型检查："
FILE_INFO=$(file "$MO_PATH")
echo "$FILE_INFO"
if echo "$FILE_INFO" | grep -q "static"; then
    echo "   ✅ 包含 'static' - 静态链接"
else
    echo "   ⚠️  未发现 'static' - 可能不是静态链接"
fi
echo ""

echo "【3】动态库依赖检查："
LDD_INFO=$(ldd "$MO_PATH" 2>&1)
echo "$LDD_INFO"
if echo "$LDD_INFO" | grep -q "statically linked"; then
    echo "   ✅ 零依赖 - 完全静态链接"
elif echo "$LDD_INFO" | grep -q "not a dynamic"; then
    echo "   ✅ 静态可执行文件"
else
    echo "   ⚠️  存在动态库依赖"
    echo ""
    echo "   依赖的库："
    echo "$LDD_INFO" | grep "=>" | sed 's/^/   /'
fi
echo ""

echo "【4】版本测试："
VERSION=$("$MO_PATH" --version 2>&1)
if [ $? -eq 0 ]; then
    echo "   ✅ 运行成功：$VERSION"
else
    echo "   ❌ 运行失败"
    echo "$VERSION"
fi
echo ""

echo "========================================="
echo "  验证总结"
echo "========================================="

# 总结
ISSUES=0

if ! echo "$FILE_INFO" | grep -q "static"; then
    echo "⚠️  文件类型不包含 'static'"
    ISSUES=$((ISSUES + 1))
fi

if ! echo "$LDD_INFO" | grep -q "statically linked"; then
    if ! echo "$LDD_INFO" | grep -q "not a dynamic"; then
        echo "⚠️  存在动态库依赖"
        ISSUES=$((ISSUES + 1))
    fi
fi

if [ "$VERSION" = "" ]; then
    echo "⚠️  无法运行或获取版本"
    ISSUES=$((ISSUES + 1))
fi

if [ $ISSUES -eq 0 ]; then
    echo ""
    echo "🎉 验证通过！mo 是完全静态链接的可执行文件"
    echo "   可以在 CentOS 7 及其他 Linux 发行版上运行"
    echo ""
    echo "部署建议："
    echo "  1. 可直接拷贝到目标系统"
    echo "  2. 无需安装任何依赖"
    echo "  3. 兼容所有 Linux x86_64 系统"
else
    echo ""
    echo "⚠️  发现 $ISSUES 个问题，建议重新编译"
    echo ""
    echo "重新编译命令："
    echo "  cd /home/apachee/trunk/Crystal/CrystalFlowAnalyzer/modb/moparse-rs"
    echo "  ./build.sh"
fi

echo "========================================="
